global !p

from builtin import choose_next
from mdtex.scopes import math

logical_terms       = {'yw': 'because', 'sy': 'therefore', 'and': 'land', 'or': 'lor', 'not': 'lnot'}
logical_terms_cycle = ['because', 'therefore', 'land', 'lor', 'lnot']

exists       = ['exists', 'nexists']
exist_forall = ['exists', 'forall']

set_relations_reverse = ['b', 'p']
set_relations_sub     = ['', 'eq', 'neqq']
set_relations_sup     = ['', 'eq', 'neqq']

not_reverse = ['', 'not']
big_reverse = ['', 'big']

a_u  = ['a', 'u']
ands = ['land', 'bigwedge']
ors  = ['lor', 'bigvee']

endglobal

################################################################
#                                                              #
#                         Logic & Set                          #
#                                                              #
################################################################

context "math()"
snippet TT "真 True" Aw
\mathbf{T}
endsnippet

context "math()"
snippet FF "假 False" Aw
\mathbf{F}
endsnippet

context "math()"
snippet "(?<!\\)\b(yw|sy|and|or|not)" "逻辑用语 Logical Terms" r
\\`!p snip.rv = logical_terms[match.group(1)]` 
endsnippet

context "math()"
snippet "\\(because|therefore|land|lor|lnot)(\s?)" "逻辑用语转换 Logical Terms Transformation" r
\\`!p snip.rv = choose_next(match.group(1), logical_terms_cycle, 5) + match.group(2)`
endsnippet

context "math()"
snippet "\\(land|bigwedge)(\s?)/" "与 And" r
\\`!p snip.rv = choose_next(match.group(1), ands, 2) + match.group(2)`
endsnippet

context "math()"
snippet "\\(lor|bigvee)(\s?)/" "或 Or" r
\\`!p snip.rv = choose_next(match.group(1), ors, 2) + match.group(2)`
endsnippet

context "math()"
snippet |-> "映射 Map" Ai
\mapsto 
endsnippet

context "math()"
snippet |_> "映射 Map" Ai
\xmapsto{${1:${VISUAL}}}$0
endsnippet

context "math()"
snippet "(?<!\\)\b(exist|EE)" "存在 Exist" Ar
\exists 
endsnippet

context "math()"
snippet "(?<!\\)\b(forall|AA)" "任意 Forall" Ar
\forall 
endsnippet

context "math()"
snippet "\\(exists|nexists)(_(?:[a-zA-Z]|\{[^}]+\}),?)(\s?)/" "存在 & 不存在 Exist & Non-Exist" r
\\`!p snip.rv = choose_next(match.group(1), exists, 2) + match.group(2) + match.group(3)`
endsnippet

context "math()"
snippet "\\(exists|forall)(_(?:[a-zA-Z]|\{[^}]+\}),?)(\s?)" "存在 & 任意 Exist & Forall" r
\\`!p snip.rv = choose_next(match.group(1), exist_forall, 2) + match.group(2) + match.group(3)`
endsnippet

context "math()"
snippet buji "补集 Complement" Aw
\complement_{${1:${VISUAL}}}$0
endsnippet

context "math()"
snippet "(?<!\\)\b(empty|kj)" "空集 Empty" r
\empty 
endsnippet

context "math()"
snippet bhb "⊂ 包含于 Subset" Aw
\subset 
endsnippet

context "math()"
snippet bhp "⊃ 包含 Supset" Aw
\supset 
endsnippet

context "math()"
snippet "\\subset((?:eq|neqq)?)(\s?)" "包含于 Subset" r
\subset`!p snip.rv = choose_next(match.group(1), set_relations_sub, 3) + match.group(2)`
endsnippet

context "math()"
snippet "\\supset((?:eq|neqq)?)(\s?)" "包含 Supset" r
\supset`!p snip.rv = choose_next(match.group(1), set_relations_sup, 3) + match.group(2)`
endsnippet

context "math()"
snippet "\\su(b|p)(set(?:eq|neqq)?)(\s?)/" "包含 & 包含于 Subset & Supset" Ar
\su`!p snip.rv = choose_next(match.group(1), set_relations_reverse, 2) + match.group(2) + match.group(3)`
endsnippet

context "math()"
snippet "(?<!\\)\b(cap|jiao)" "交集 Intersection" Ar
\cap 
endsnippet

context "math()"
snippet "(?<!\\)\b(cup|bing)" "并集 Union" Ar
\cup 
endsnippet

context "math()"
snippet "\\((?:big)?)(cap|cup)(\s?)" "交集 & 并集 Intersection & Union" r
\\`!p snip.rv = choose_next(match.group(1), big_reverse, 2) + match.group(2) + match.group(3)`
endsnippet

context "math()"
snippet in "属于 In" w
\in 
endsnippet

context "math()"
snippet "(?<!\\)(\||\\mid)" "整除 Exact Devision" r
`!p snip.rv = choose_next(match.group(1), ['|', '\\mid'], 2)`
endsnippet

priority 1
context "math()"
snippet "(\\(?:big)?c)(a|u)p(\s?)/" "交集 & 并集 Intersection & Union" Ar
`!p snip.rv = match.group(1) + choose_next(match.group(2), a_u, 2) + match.group(3)`p
endsnippet

context "math()"
snippet "\\((?:not)?)(in|ni)(\s?)" "属于转换 In Transforamtion" r
\\`!p snip.rv = choose_next(match.group(1), not_reverse, 2) + match.group(2) + match.group(3)`
endsnippet

context "math()"
snippet "\\(in|ni)(\s?)/" "属于转换 In Transforamtion" Ar
\\`!p snip.rv = match.group(1)[::-1] + match.group(2)`
endsnippet
